#!/bin/bash -e

FAN_STATUS_UNKNOWN="Unknown"
FAN_STATUS_NORMAL="Normal"
FAN_STATUS_ABNORMAL="Abnormal"

set_fan_led_abnormal()
{
    local fcb_num=$1
    local fan_num=$2

    busctl set-property "xyz.openbmc_project.LED.GroupManager" \
        "/xyz/openbmc_project/led/groups/fcb${fcb_num}fan${fan_num}_amber" \
        "xyz.openbmc_project.Led.Group" "Asserted" "b" "true"

    busctl set-property "xyz.openbmc_project.LED.GroupManager" \
        "/xyz/openbmc_project/led/groups/fcb${fcb_num}fan${fan_num}_blue" \
        "xyz.openbmc_project.Led.Group" "Asserted" "b" "false"
}

set_fan_led_normal()
{
    local fcb_num=$1
    local fan_num=$2

    busctl set-property "xyz.openbmc_project.LED.GroupManager" \
        "/xyz/openbmc_project/led/groups/fcb${fcb_num}fan${fan_num}_amber" \
        "xyz.openbmc_project.Led.Group" "Asserted" "b" "false"

    busctl set-property "xyz.openbmc_project.LED.GroupManager" \
        "/xyz/openbmc_project/led/groups/fcb${fcb_num}fan${fan_num}_blue" \
        "xyz.openbmc_project.Led.Group" "Asserted" "b" "true"
}

check_fcb_fan_status()
{
    local fcb_num=$1

    if ! [[ $fcb_num =~ ^[0-1]$ ]]; then
        echo "$FAN_STATUS_UNKNOWN"
        return 1
    fi

    local service="xyz.openbmc_project.FanSensor"
    local interface="xyz.openbmc_project.Sensor.Threshold.Critical"
    local property_list=("CriticalAlarmHigh" "CriticalAlarmLow")
    local in_out=("IL" "OL")

    for index in {0..1}
    do
        local fan_number=$(((fcb_num * 2) + index ))
        local NoCritical=0
        local object_path="/xyz/openbmc_project/sensors/fan_tach/FCB_${fcb_num}_FAN${fan_number}_TACH_${in_out[$io_index]}_SPEED_RPM"

        for property in "${property_list[@]}"; do
            tach_alarm=$(busctl get-property "$service" "$object_path" "$interface" "$property" | cut -d ' ' -f2)
            if [ "$tach_alarm" = "true" ]; then
                set_fan_led_abnormal "${fcb_num}" "${fan_number}"
                ((NoCritical++))
                echo "$FAN_STATUS_ABNORMAL"
                break;
            fi
        done

        if [ "$NoCritical" = "0" ]; then
            set_fan_led_normal "${fcb_num}" "${fan_number}"
        fi

        # Check FCB whether is AC off
        local operation_interface="xyz.openbmc_project.State.Decorator.OperationalStatus"
        functional=$(busctl get-property "$service" "$object_path" "$operation_interface" Functional | awk '{print $NF}')
        if [ "$functional" = "false" ]; then
            set_fan_led_abnormal "${fcb_num}" "${fan_number}"
            echo "$FAN_STATUS_ABNORMAL"
            return 0
        fi
    done

    echo "$FAN_STATUS_NORMAL"
    return 0
}

while true
do
    fan_status=$FAN_STATUS_NORMAL

    # FCB0 ~ FCB1
    for i in {0..1}
    do
        # Check each sensor on the FCB whether alert critical alarm
        fan_status=$(check_fcb_fan_status "$i")
        if [ "$fan_status" = "$FAN_STATUS_ABNORMAL" ]; then
            break
        fi
    done

    sleep 1
done
